﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using TwitterLikeWebChatDAL;

namespace TwitterLikeWebChatWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class TwitterService : ITwitter
    {
        public string[] GetChannelNames()
        {
            string[] channelNames = TwitterLikeWebChatDAO.GetChannelNames();
            return channelNames;
        }

        public MessageDTO[] GetMessagesByChannelName(string channelName)
        {
            Message[] messages = TwitterLikeWebChatDAO.GetMessagesByChannelName(channelName);
            MessageDTO[] messagesDTO = new MessageDTO[messages.Length];

            for (int i = 0; i < messages.Length; i++)
            {                
                MessageDTO msgDTO = new MessageDTO();
                msgDTO.MessageId = messages[i].MessageId;
                msgDTO.MessageContent = messages[i].MessageContent;
                msgDTO.PostDateTime = messages[i].PostDateTime;

                if (i == 0)
                {
                    msgDTO.ChannelName = channelName;
                }

                messagesDTO[i] = msgDTO;
            }

            return messagesDTO;            
        }

        public void DeleteAllMessagesByChannelName(string channelName)
        {
            TwitterLikeWebChatDAO.DeleteAllMessagesByChannelName(channelName);
        }       

        public void UploadMessageByChannelName(MessageDTO messageDTO)
        {            
            TwitterLikeWebChatDAO.UploadMessageByChannelName(messageDTO.ChannelName, messageDTO.MessageContent);
        }

        public MessageDTO[] GetLastNMessagesByChannelName(string channelName, string maxMessagesCountString)
        {
            int maxMessagesCount;

            if (Int32.TryParse(maxMessagesCountString, out maxMessagesCount))
            {
                Message[] messages = TwitterLikeWebChatDAO.GetMessagesByChannelName(channelName, maxMessagesCount);
                MessageDTO[] messagesDTO = new MessageDTO[messages.Length];

                for (int i = 0; i < messages.Length; i++)
                {
                    MessageDTO msgDTO = new MessageDTO();
                    msgDTO.MessageId = messages[i].MessageId;
                    msgDTO.MessageContent = messages[i].MessageContent;
                    msgDTO.PostDateTime = messages[i].PostDateTime;

                    if (i == 0)
                    {
                        msgDTO.ChannelName = channelName;
                    }

                    messagesDTO[i] = msgDTO;
                }

                return messagesDTO;
            }
            else
            {
                throw new ArgumentException("maxMessagesCountString");
            }
        }

        public void CreateNewChannel(string channelName)
        {
            TwitterLikeWebChatDAO.CreateNewChannel(channelName);
        }

        public void DeleteChannel(string channelName)
        {
           TwitterLikeWebChatDAO.DeleteChannel(channelName);           
        }
    }
}
